/**
 * Author            : xiaogebin <565127608@qq.com>
 * Date              : 26.12.2024
 * Last Modified Date: 26.12.2024
 * Last Modified By  : xiaogebin <565127608@qq.com>
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

void process_bar(int part, int total)
{
    const int MAX_NUM = 40;
    int kedu = 1;
    int n = (float)((float)part / total) * MAX_NUM;
    int start = 4;
    int end;
    /* printf("%d%%", (int)((float)1/100 * 100)); */
    /* printf("%d%%\n", (int)(4.0 / 80 * 100)); */
    /* return 0; */
    /* while (1) { */
    int i, j;
    /* if (n >= MAX_NUM) { */
    /*     n = 0; */
    /* } */
    char buf[100] = { 0 };
    /* printf("%d%%", 100 * n / MAX_NUM); */
    sprintf(buf, "%d%%", (int)((float)n / MAX_NUM * 100));
    system("clear");
    /* printf("n: %d\n", n); */
    /* printf("buf: %s\n", buf); */
    /*
     * 红色的索引 - 4 = 可以显示的长度
     */
    int show_long_red; // 表示显示红色部分的文字长度
    char buf_show_red[200] = { 0 };
    int show_long_green; // 表示显示绿色部分的文字长度
    char buf_show_green[200] = { 0 };
    char* p = buf_show_red;
    char* p_green = buf_show_green;
    end = start + strlen(buf) - 1;
    /* if (end < n) */
    if (n < start) {
        show_long_red = 0;
        for (i = 0; i < n; i++) {
            *p++ = ' ';
        }
    } else if (n >= start && n <= end) {
        show_long_red = n - start + 1;
        for (i = 0; i <= start - 1; i++) {
            *p++ = ' ';
        }
        for (i = 0; i < show_long_red; i++) {
            *p++ = buf[i];
        }
        for (i = end + 1; i <= n; i++) {
            *(p++) = ' ';
        }
    } else if (n > end) {
        show_long_red = strlen(buf);
        for (i = 0; i <= start - 1; i++) {
            *p++ = ' ';
        }
        for (i = 0; i < show_long_red; i++) {
            *p++ = buf[i];
        }
        for (i = end + 1; i <= n; i++) {
            *(p++) = ' ';
        }
    }

    /* printf("n:%d\n", n); */

    /* printf("xxx %d xxx", n); */
    printf("\033[41m%s\033[0m", buf_show_red);

    /* for (j = n; j < MAX_NUM; ++j) { */
    /*     printf("\033[42m \033[0m"); */
    /* } */
    /* char* p_green = buf_show_green; // 用于存放绿色的部分 */
    /* if (n > end) */
    /*     show_long_green = 0; */
    /* else */
    /*     show_long_green = end - n; */
    /*  */
    /* for (i = n - start + 1; i < strlen(buf); i++) { */
    /*     *p_green++ = buf[i]; */
    /* } */
    /*  */
    /* for (i = 0; i < MAX_NUM - end; ++i) { */
    /*     *p_green++ = ' '; */
    /*     i++; */
    /* } */
    /* p = buf_show_green; */
    if (n < start) {
        show_long_green = 0;
        for (i = n + 1; i < start; i++) {
            *p_green++ = ' ';
        }

        for (i = 0; i < strlen(buf); i++) {
            *p_green++ = buf[i];
        }

        for (i = end + 1; i < MAX_NUM; i++) {
            *p_green++ = ' ';
        }
    } else if (n >= start && n <= end) {
        show_long_green = end - n + 1;
        for (i = 0; i < end - n; i++) {
            *p_green++ = buf[i + n - start + 1];
        }
        for (i = end + 1; i < MAX_NUM; i++) {
            *(p_green++) = ' ';
        }
    } else if (n > end) {
        show_long_green = strlen(buf);
        for (i = n + 1; i < MAX_NUM; i++) {
            *(p_green++) = ' ';
        }
    }
    printf("\033[42m%s\033[0m", buf_show_green);

    printf("\n");
    /* n++; */
    /* n++; */
    /* sleep(2); */
    /* } */
}


int copy_file(const char* srcfile, const char* destfile)
{
    int fd_src = open(srcfile, O_RDONLY);
    if (-1 == fd_src) {
        perror("failed to open file, error:");
        return 1;
    }
    /* printf("open file which is use to read success\n"); */
    mode_t mode = S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
    int fd_dest = open(destfile, O_WRONLY | O_TRUNC | O_CREAT, mode);
    if (-1 == fd_dest) {
        perror("failed to open file, error:");
        close(fd_src);
        return 1;
    }
    // 获取字节数
    struct  stat status_file;
    stat(srcfile, &status_file);
    /* printf("%d\n", status_file.st_size); */
    /* printf("sucess open the write file!"); */
    long filesize = status_file.st_size;
    long sum = 0;
    while (1) {
        char buf[1024] = { 0 };
        int c = read(fd_src, buf, 10);
        if (c == -1) {
            perror("read file error\n");
            close(fd_dest);
            close(fd_src);
            return 1;
        }
        if (c == 0) {
            // 读取结束
            break;
        }
        int cw = write(fd_dest, buf, c);
        if (cw == -1) {
            perror("write file error\n");
            close(fd_dest);
            close(fd_src);
            return 1;
        }
        sum += cw;
        process_bar(sum, filesize);
        /* sleep(1); */
    }
    close(fd_dest);
    close(fd_src);
    printf("复制成功\n");
    return 0;
}

int main(int argc, char* argv[])
{
    /* for (int i = 0; i <= 10000; i++) { */
    /*     process_bar(i, 10000); */
    /* } */
    copy_file("./test.c", "2.c");
    return 0;
}
